<template>
	<div style="padding: 0 24rpx;padding-top: 40rpx;">
		<view style="display: flex;justify-content: space-between;align-items: center;">
			<view style="width: 120rpx;text-align-last: justify;">用户名:</view>
			<input v-model="account" type="text" placeholder="用户名">
		</view>
		<view style="display: flex;justify-content: space-between;align-items: center;margin-top: 14rpx;">
			<view style="width: 120rpx;text-align-last: justify;">密码:</view>
			<input v-model="password" type="password" placeholder="密码">
		</view>

		<button @click="login">登录</button>
	</div>
</template>

<script>
	import Web3 from 'web3';
	import {
		getLogin
	} from '@/api/index.js'
	export default {
		data() {
			return {
				account: '',
				password: '',
				accountsUrl: null,
			};
		},
		onLoad() {
			this.getcurentAddress()
		},
		methods: {
			// 获取当前钱包
			async getcurentAddress() {
				// debugger
				const accounts = await ethereum.request({
					method: 'eth_requestAccounts'
				});
				this.accountsUrl = accounts[0]
				uni.setStorageSync('accountsUrl', accounts[0])
				console.log(accounts[0], '获取当前钱包')
			},
			login() {
				if (!this.account) {
					uni.showToast({
						title: '请输入用户名',
						icon: "none",
						duration: 2000
					})
				} else if (!this.password) {
					uni.showToast({
						title: '请输入密码',
						icon: "none",
						duration: 2000
					})
				} else if (this.accountsUrl != null) {
					getLogin({
							// account:this.account,
							// password:this.password,
							url: this.accountsUrl,
						},
						(data) => {
							// 处理成功返回的数据
							console.log(data.userinfo.token, 'data')
							// let token = uni.setStorageSync('token', data.userinfo.token)
							const translate = uni.getStorageSync('locale')
							if (!translate) {
								uni.setStorageSync('locale', 'tr-CN')
							}
							uni.setStorage({
								key: 'token',
								data: data.userinfo.token,
								success: function() {
									console.log('success');
									uni.redirectTo({
										url: '/pages/index/index'
									})
								}
							});


						},
						(errorMsg) => {
							// 处理请求失败或错误的情况
							console.error(errorMsg);
						},
					);
				}

			}
		}
	}
</script>
<style scoped>
	view input {
		width: 560rpx;
		height: 80rpx;
		border: 1px solid #ddd;
		margin-top: 16rpx;
		border-radius: 16rpx;
		padding-left: 16rpx;
		box-sizing: border-box;
	}

	button {
		margin-top: 60rpx;
		background-color: #0762FF;
		color: #ffffff;
	}
</style>